package cn.exrick.xboot.modules.base.serviceimpl.mybatis;

import cn.exrick.xboot.common.constant.CommonConstant;
import cn.exrick.xboot.modules.base.dao.mapper.UserRoleMapper;
import cn.exrick.xboot.modules.base.entity.Role;
import cn.exrick.xboot.modules.base.entity.User;
import cn.exrick.xboot.modules.base.entity.UserRole;
import cn.exrick.xboot.modules.base.service.RoleService;
import cn.exrick.xboot.modules.base.service.UserService;
import cn.exrick.xboot.modules.base.service.mybatis.IUserRoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Exrickx
 */
@Service
@Slf4j
public class IUserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements IUserRoleService {
    @Autowired
    private IUserRoleService iUserRoleService;
    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @Override
    public List<Role> findByUserId(String userId) {

        return userRoleMapper.findByUserId(userId);
    }

    @Override
    public List<String> findDepIdsByUserId(String userId) {

        return userRoleMapper.findDepIdsByUserId(userId);
    }
    /**
     * 通过roleId查询user
     * @param userRole
     * @return
     */
    @Override
    public  List<UserRole> findUserRoleByRoleId(UserRole userRole){
        return userRoleMapper.findUserRoleByRoleId(userRole);
    }

    /**
     * 通过userId查询user
     * @param userRole
     * @return
     */
    @Override
   public  List<UserRole> findUserRoleByUserId(UserRole userRole){
        return userRoleMapper.findUserRoleByUserId(userRole);
    }

    /**
     * njp
     * 通过权限id获取用户名
     * @return
     * 2019.10.09
     */
    @Override
    public List<User>  findUsernamesByRoleId(@Param("roleId") String roleId){
        return userRoleMapper.findUsernamesByRoleId(roleId);
    }

    /**
     * njp
     * 验证该用户是否是审批员
     * user中需要username
     * @return
     * 2019.10.15
     */
    @Override
   public boolean  getApprovalRole(User user){
       try{
        //1.获取user id
       String userId = userService.findByUsername(user.getUsername()).getId();
       //2.根据userID获取该用户的权限
       UserRole   userRole= new UserRole();
       userRole.setUserId(userId);
       List<UserRole> userRoleByUserId = findUserRoleByUserId(userRole);
           for (UserRole role : userRoleByUserId) {
               if(CommonConstant.APPROVAL_ROLRID.equals(role.getRoleId())){
                   log.info("该用户是审批员");
                   return true;
               }
           }
       }catch (NullPointerException n){
           log.info("长连接username为管理员");
       }
       catch (Exception e){
           e.printStackTrace();
           return false;
       }
       return false;
   }

    @Override
    public List<Role> findByUserIdAndType(String userId, Integer type) {
        return userRoleMapper.findByUserIdAndType(userId,type);
    }

    @Override
    public void deleteUserRoleInIds(User u) {
        //获取全部管理员角色
        List<Role> roles = roleService.findByType(CommonConstant.ROLE_TYPE_ADMIN);
        LambdaQueryWrapper<UserRole> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(UserRole::getUserId,u.getId());
        List<String> roleIds=new ArrayList<>();
        roles.forEach(role -> {
            roleIds.add(role.getId());
        });
        queryWrapper.in(UserRole::getRoleId,roleIds);
        this.remove(queryWrapper);
        List<Role> userRoles = iUserRoleService.findByUserId(u.getId());
        //判断是否需要设置默认角色
        if(userRoles!=null&&userRoles.size()>0){
          return;
        }
        // 设置默认角色
        List<Role> roleList = roleService.findByDefaultRole(true);
        if(roleList!=null&&roleList.size()>0){
            for(Role role : roleList){
                UserRole ur = new UserRole();
                ur.setUserId(u.getId());
                ur.setRoleId(role.getId());
                iUserRoleService.save(ur);
            }
        }


    }

}
